<?php

namespace App\Console\Commands;

use App\Models\Admin\Goods;
use App\Models\Admin\GoodsStore;
use App\Models\Api\Store;
use Illuminate\Console\Command;

class Tbgoods extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tbgoods';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'tbgoods';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * @return mixed
     *  `id` int(11) NOT NULL AUTO_INCREMENT,
     * `store_id` int(11) NOT NULL COMMENT '商户ID',
     * `goods_id` int(11) NOT NULL DEFAULT '0' COMMENT '商品ID',
     * `sale_price` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT '商品金额',
     * `buy_num` int(11) NOT NULL DEFAULT '1' COMMENT '购买数量',
     * `get_num` int(11) NOT NULL DEFAULT '1' COMMENT '实际得到数量',
     * `created_at` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
     * `updated_at` int(11) NOT NULL DEFAULT '0',
     */
    public function handle()
    {
        $goodsList = Goods::select(['id', 'goods_price', 'fixed_price'])->where('created_at', '>', 0)->get()->toArray();
        if ($goodsList) {
            $nowTime = time();
            $storeList = Store::where([])->select(['id', 'discount_rate'])->get()->toArray();
            foreach ($storeList as $store) {
                foreach ($goodsList as $kk => $goods) {
                    $id = GoodsStore::where(['goods_id' => $goods['id'], 'store_id' => $store['id']])->value('id');
                    v($id);
                    if (!$id) {
                        $info['store_id'] = $store['id'];
                        $info['goods_id'] = $goods['id'];
                        $info['sale_price'] = $goods['goods_price'] * $store['discount_rate'];
                        $info['buy_num'] = 1;
                        $info['get_num'] = 1;
                        $info['created_at'] = $nowTime;
                        GoodsStore::insert($info);
                    }
                }
            }
        }
        echo "更新商品数量：" . count($goodsList);
    }
}
